package wifiunion.com.notice.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import wifiunion.com.notice.Constants;
import wifiunion.com.notice.GlideCircleTransform;
import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.ConsumerGroup;
import wifiunion.com.notice.entity.MemberChild;


/**
 * 作 者: 
 * 创建时间: 2013-12-9 上午11:05:35<br>
 * 版权声明: Copyright (C) 2006-2013 <br>
 * 修改历史: 2013-12-9 1.0.0 初始版本<br>
 * 文件描述: 查看报告
 */
public class AddToMembersAdapter extends BaseExpandableListAdapter {


	private Context mContext;

	private ArrayList<ConsumerGroup> flist  = new ArrayList<ConsumerGroup>();
	private View.OnClickListener clickListener;

	public AddToMembersAdapter(Context mContext,ArrayList<ConsumerGroup> gplist,View.OnClickListener clickListener) {
		super();
		this.mContext = mContext;
		this.flist = gplist;
		this.clickListener = clickListener;

	}

	public void setData(ArrayList<ConsumerGroup> flist) {
		this.flist = flist;
	}

	@Override
	public int getGroupCount() {
		if (null != flist) {
			return flist.size();
		} else {
			return 0;
		}

	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (null != flist && null != flist.get(groupPosition)
				&& null != flist.get(groupPosition).getMemberList()) {
			return flist.get(groupPosition).getMemberList().size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getGroup(int groupPosition) {
		if(null != flist && flist.size() >0 )
		{
			return flist.get(groupPosition);
		}
		else
		{
			return 0;
		}
//		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return flist.get(groupPosition).getMemberList().get(childPosition);

//		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
							 View convertView, ViewGroup parent) {
		// 实例化布局文件

		AddToMembersAdapter.ParentViewHolder parentViewHolder = null;

		if (convertView == null) {
			convertView = LayoutInflater.from(mContext)
					.inflate(R.layout.parent_usergroup_item, null);
			parentViewHolder = new AddToMembersAdapter.ParentViewHolder();
//			parentViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.pater_selectall_cb);
			parentViewHolder.nameTv = (TextView) convertView.findViewById(R.id.pater_name_tv);
//			parentViewHolder.stateIv = (ImageView) convertView.findViewById(R.id.pater_state_iv);
			convertView.setTag(parentViewHolder);
		} else {
			parentViewHolder = (AddToMembersAdapter.ParentViewHolder) convertView.getTag();
		}
//		parentViewHolder.checkBox.setOnClickListener(clickListener);
//		parentViewHolder.checkBox.setTag(groupPosition);


		ConsumerGroup gp = (ConsumerGroup) flist.get(groupPosition);


//		if (gp.isChecked()) {
//			parentViewHolder.checkBox.setSelected(true);
//		} else {
//			parentViewHolder.checkBox.setSelected(false);
//		}
		parentViewHolder.nameTv.setText(gp.getName());


//		RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext)
//				.inflate(R.layout.pater_group_item, null);
//
//		ImageView stateIv = (ImageView) glayout.findViewById(R.id.pater_state_iv);
//		TextView  nameTv  = (TextView) glayout.findViewById(R.id.pater_name_tv);
//		ImageView parentIv = (ImageView) glayout.findViewById(R.id.pater_all_iv);
//		parentIv.setTag(groupPosition+"");
//		ConsumerGroup gp  = (ConsumerGroup)flist.get(groupPosition);
//
//		if (isExpanded) {
//			parentViewHolder.stateIv .setImageResource(R.drawable.pater_open);
//
//		} else {
//			parentViewHolder.stateIv .setImageResource(R.drawable.pater_close);
//
//		}
//        if(gp.isChecked())
//		{
//			parentIv.setSelected(true);
//		}
//		else
//		{
//			parentIv.setSelected(false);
//		}
//		nameTv.setText(gp.getName());
//
//		parentIv.setOnCheckedChangeListener(mCheckedChangeListener);


		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
							 boolean isLastChild, View convertView, ViewGroup parent) {
		// 实例化布局文件

		AddToMembersAdapter.ChildViewHolder childViewHolder = null;

		if (convertView == null) {
			convertView = LayoutInflater.from(mContext)
					.inflate(R.layout.child_member_item, null);
			childViewHolder = new AddToMembersAdapter.ChildViewHolder();
			childViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.child_selectall_cb);
			childViewHolder.nameTv = (TextView) convertView.findViewById(R.id.child_member_name);
			childViewHolder.mainView = (RelativeLayout) convertView.findViewById(R.id.member_addgroup_subitem_rl);
			childViewHolder.childHeadIv = (ImageView) convertView.findViewById(R.id.child_head_iv);
			convertView.setTag(childViewHolder);
		} else {
			childViewHolder = (AddToMembersAdapter.ChildViewHolder) convertView.getTag();
		}
//		childViewHolder.checkBox.setOnClickListener(clickListener);
//		childViewHolder.checkBox.setTag(groupPosition+"_"+childPosition);
		childViewHolder.mainView.setTag(groupPosition+"_"+childPosition);
		childViewHolder.mainView.setOnClickListener(clickListener);


		MemberChild md = (MemberChild) flist.get(groupPosition).getMemberList().get(childPosition);

		if (md.getIsSelected()==1) {
			childViewHolder.checkBox.setSelected(true);
		} else {
			childViewHolder.checkBox.setSelected(false);
		}

		if(!TextUtils.isEmpty(md.getShowPic()))
		{

			Glide.with(mContext)
					.load(Constants.HTTP_URL_IMAGESERVER + md.getShowPic())
					.placeholder(R.drawable.mr_head).error(R.drawable.mr_head).fitCenter()
					.transform(new GlideCircleTransform(mContext)).into(childViewHolder.childHeadIv );
		}
		else
		{
			Glide.with(mContext)
					.load(Constants.HTTP_URL_IMAGESERVER + md.getComparisonPic())
					.placeholder(R.drawable.mr_head).error(R.drawable.mr_head).fitCenter()
					.transform(new GlideCircleTransform(mContext)).into(childViewHolder.childHeadIv );

		}

		childViewHolder.nameTv.setText(md.getName());




//		RelativeLayout clayout = (RelativeLayout) LayoutInflater.from(mContext)
//				.inflate(R.layout.child_member_item, null);
//		ImageView choooseIv  = (ImageView) clayout.findViewById(R.id.child_chooseitem__iv);
//		choooseIv.setTag(groupPosition+"_"+childPosition);
//		choooseIv.setOnClickListener(onClickListener);
//		TextView name = (TextView) clayout.findViewById(R.id.chil_member_name);
//		MemberChild md  = (MemberChild)flist.get(groupPosition).getMemberList().get(childPosition);
//		name.setText(md.getName());
//
//		if(md.getIsSelected()  == 1)
//		{
//			choooseIv.setSelected(true);
//		}
//		else
//		{
//			choooseIv.setSelected(false);
//		}


		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public class ParentViewHolder {
		private TextView nameTv;
//		private CheckBox checkBox;
		private ImageView stateIv;
	}

	public class ChildViewHolder {
		private RelativeLayout mainView;
		private TextView nameTv;
		private ImageView childHeadIv;
		private CheckBox checkBox;
	}
}